﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;


namespace MII_FIF.Layouts.MII_FIF.Master.NAS
{
    public partial class AddNAS : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Generic.IsUserInGroup(Configuration.MasterGroupName))
            {
                if (Request.QueryString["ID"] != null)
                    ((Label)Header.FindControl("lblHeader")).Text = "Edit NAS";
                else
                    ((Label)Header.FindControl("lblHeader")).Text = "Add NAS";
                btnCancel.Click += btnCancel_Click;
                btnSubmit.Click += btnSubmit_Click;

                if (!IsPostBack)
                {
                    if (Request.QueryString["ID"] != null)
                    {
                        ViewState["ID"] = Request.QueryString["ID"].ToString();
                        InventoryManagement.NAS IMNAS = new InventoryManagement.NAS();
                        IMNAS.ID = Guid.Parse(ViewState["ID"].ToString());
                        IMNAS.Select();
                        txtNAS.Text = IMNAS.Title;
                    }
                }
            }
            else
            {
                Response.Redirect(Generic.GenLink("/"));
            }
        }

        void btnSubmit_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            bool check = true;
            InventoryManagement.NAS NAS = new InventoryManagement.NAS();
            if (txtNAS.Text.Trim() == string.Empty)
            {
                check = false;
                lblError.Text += "Please Insert NAS Type <br />";
            }
            if (check)
            {
                NAS.Title = txtNAS.Text;
                NAS.IsDeleted = false;
                if (Request.QueryString["ID"] == null)
                {
                    NAS.ID = Guid.NewGuid();
                    int stat = NAS.Insert();
                    if (stat > 0)
                        Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/NAS/ViewNAS.aspx"));
                    else
                        lblError.Text += "Insert Failed";
                }
                else
                {
                    NAS.ID = Guid.Parse(Request.QueryString["ID"].ToString());
                    NAS.Select();
                    if (NAS.IsDeleted.Value)
                    {
                        lblError.Text += "This item is already deleted <br />";
                    }
                    else
                    {
                        NAS.Title = txtNAS.Text;
                        NAS.IsDeleted = false;
                        int stat = NAS.Update();
                        if (stat > 0)
                            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/NAS/ViewNAS.aspx"));
                        else
                            lblError.Text += "Update Failed";
                    }
                }
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/Master/NAS/ViewNAS.aspx"));
        }
    }
}
